/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is JsWs.
 *
 * The Initial Developer of the Original Code is
 * Chris Winberry.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */

var jsws = {
	running: false,
	windowLoaded: false,

	onLoad: function (evt) {
		this.windowLoaded = !!evt;

		this.daemon = null;

		this.initialized = true;
		this.strings = document.getElementById("jsws-strings");

		var prefManager = Cc["@mozilla.org/preferences-service;1"]
			.getService(Ci.nsIPrefBranch);
		var autostart = prefManager.getBoolPref(JSWS_PREF_AUTOSTART);
		if (autostart)
			this.startServer();
	},

	onUnload: function() {
		this.stopServer();
	},

	log: function (s) {
		Cc["@mozilla.org/consoleservice;1"]
			.getService(Ci.nsIConsoleService)
			.logStringMessage("[JsWs] " + s);
	},

	alert: function (s) {
		Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
			.getService(Components.interfaces.nsIPromptService)
			.alert(window, "[JsWs]", s);
	},

	updateStatus: function () {
		if (!this.windowLoaded)
			return;
		try {
			document.getElementById('jsws-menu-start')
				.setAttribute("disabled", this.running);
			document.getElementById('jsws-menu-stop')
				.setAttribute("disabled", !this.running);
			document.getElementById('jsws-menu-startpage')
				.setAttribute("disabled", !this.running);
			var tbButton = document.getElementById('jsws-toolbar-button');
			if (tbButton != null) {
				tbButton.label = this.strings.getString(
					this.running ? "jsws.toolbarbutton.label.stop" : "jsws.toolbarbutton.label.start"
					);
				tbButton.setAttribute("status", this.running ? "on" : "off");
			}
			
			var i = 0;
			var moduleEntry = document.getElementById('jsws-menu-modules-popup').firstChild;
			while (moduleEntry && i++ < 10) {
				moduleEntry.setAttribute("disabled", !this.running);
				moduleEntry = moduleEntry.nextSibling;
			}
		} catch (e) { this.log(e); }
	},

	startServer: function () {
		try {
			var prefManager = Cc["@mozilla.org/preferences-service;1"]
				.getService(Ci.nsIPrefBranch);
			var port = prefManager.getIntPref(JSWS_PREF_PORT);
			var localOnly = prefManager.getBoolPref(JSWS_PREF_LOCALONLY);
			var threaded = prefManager.getBoolPref(JSWS_PREF_THREADED);

			if (this.daemon == null) {
				this.daemon = new JsWs(port, localOnly, threaded);
				this.daemon.start();
			}

			this.running = true;
			this.updateStatus();
		} catch (e) { this.log(e); this.alert(e); return(false); }
		return(true);
	},

	stopServer: function () {
		try {
			if (this.daemon != null) {
				this.daemon.stop();
				this.daemon = null;
			}

			this.running = false;
			this.updateStatus();
		} catch (e) { this.log(e); this.alert(e); return(false); }
		return(true);
	},

	toggleServer: function () {
		if (this.running)
			this.stopServer();
		else
			this.startServer();
	},

	launchModule: function (moduleId) {
		try {
			if (!this.running)
				return;
	
			var module = this.daemon.modules[moduleId];
			if (module == null)
				return;
			
			var url = "http://localhost:" +
				this.daemon.port +
				((module.virtualPath != "/") ? module.virtualPath : "") +
				module.startPage;
	
			/*
			var referer = new Cc("@mozilla.org/network/standard-url;1", "nsIURI");
			referer.spec = url;
			var tab = getBrowser().addTab(url, referer, null, null);
			getBrowser().selectedTab = tab;
			
			getBrowser().webNavigation.loadURI(
				url,
				Ci.nsIWebNavigation.LOAD_FLAGS_NONE,
				referer,
				null,
				null
				);
			*/
			getBrowser().selectedTab = getBrowser().addTab(url, null, null, null);
		} catch (e) { this.log(e); this.alert(e); return(false); }
		return(true);
	}

};

window.addEventListener("load", function(e) { jsws.onLoad(e); }, false);
window.addEventListener("unload", function(e) { jsws.onUnload(e); }, false);

jsws.onload();
